//
//  newsDetailViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "newsDetailViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#import "UMSocial.h"
@interface newsDetailViewController ()<UIWebViewDelegate,UMSocialUIDelegate>

@property(nonatomic, retain)UIActivityIndicatorView  *activityIndicator;
@property(nonatomic, retain)UIView *myView;
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)UIView *footView;
@end

@implementation newsDetailViewController
- (void)dealloc
{   [_webView release];
    [_activityIndicator release];
    [_myView release];
    [_footView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.myId);
    // Do any additional setup after loading the view.
    //加载webview
     self.webView  = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT)];
    //self.webView.scalesPageToFit = YES;
    self.webView.delegate = self;
    //[self.webView setOpaque:NO];//opaque是不透明的意思
    //[self.webView setScalesPageToFit:YES];//自动缩放以适应屏幕
    NSString *string = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html",self.myId];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    [self.webView loadRequest:request];//加载
    //self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
    [self.webView release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fx"] style:UIBarButtonItemStylePlain target:self action:@selector(itemOne)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sc_n"] style:UIBarButtonItemStylePlain target:self action:@selector(itemTwo)];
    self.navigationItem.rightBarButtonItems = @[item1,item2];
    
    //给h5添加尾视图(KVO)
    [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:nil];
}
//监听结果回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    //在这里面添加代码
    //这里是添加在h5上面的
    [self addView];
}
- (void)addView{
    //取消监听，因为这里会调整contentSize，避免无限递归
    [self removeObserverForWebViewContentSize];
    //UIView *view = [self.view viewWithTag:99999];
    CGSize contentSize = self.webView.scrollView.contentSize;
    self.footView = [[UIView alloc]init];
    self.footView.backgroundColor = [UIColor whiteColor];
    self.footView.userInteractionEnabled = YES;
    self.footView.frame = CGRectMake(0, contentSize.height, WIDTH, 130);
    [_footView release];
    
    UILabel *xlable = [UILabel new];
    xlable.backgroundColor = [UIColor blackColor];
    [self.footView addSubview:xlable];
    [xlable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(2);
    }];
    [xlable release];
    UILabel *Tlable = [UILabel new];
    Tlable.backgroundColor = [UIColor whiteColor];
    [self.footView addSubview:Tlable];
    [Tlable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(80);
        make.left.mas_equalTo(xlable.mas_right).offset(10);
    }];
    [Tlable release];
    Tlable.text = @"分享";
    
    UIButton *zanBtn = [self cerateBtn];
    UIButton *weiBtn = [self cerateBtn];
    UIButton *quanBtn = [self cerateBtn];
    UIButton *xinBtn = [self cerateBtn];
//    [zanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(55);
//        make.left.mas_equalTo(20);
//        make.width.mas_equalTo((WIDTH - 160) / 4);
//        make.bottom.mas_equalTo(-15);
//    }];
    [weiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(zanBtn);
//        make.left.mas_equalTo(zanBtn.mas_right).offset(40);
//        make.width.mas_equalTo(zanBtn);
//        make.bottom.mas_equalTo(zanBtn);
        make.top.mas_equalTo(55);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo((WIDTH - 160) / 3);
        make.bottom.mas_equalTo(-15);
        
    }];
    [quanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weiBtn);
        make.left.mas_equalTo(weiBtn.mas_right).offset(40);
        make.width.mas_equalTo(weiBtn);
        make.bottom.mas_equalTo(weiBtn);
        
    }];
    [xinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weiBtn);
        make.left.mas_equalTo(quanBtn.mas_right).offset(40);
        make.width.mas_equalTo(weiBtn);
        make.bottom.mas_equalTo(weiBtn);
        make.right.mas_equalTo(-20);
    }];
    
    //[zanBtn setImage:[UIImage imageNamed:@"123-1"] forState:UIControlStateNormal];
    [weiBtn setImage:[UIImage imageNamed:@"117"] forState:UIControlStateNormal];
    [quanBtn setImage:[UIImage imageNamed:@"119"] forState:UIControlStateNormal];
    [xinBtn setImage:[UIImage imageNamed:@"118"] forState:UIControlStateNormal];
    
    //[zanBtn addTarget:self action:@selector(zanAction) forControlEvents:UIControlEventTouchUpInside];
    [weiBtn addTarget:self action:@selector(weiAction) forControlEvents:UIControlEventTouchUpInside];
    [quanBtn addTarget:self action:@selector(weiAction) forControlEvents:UIControlEventTouchUpInside];
    [xinBtn addTarget:self action:@selector(weiAction) forControlEvents:UIControlEventTouchUpInside];
    

    [self.webView.scrollView addSubview:self.footView];
    self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, contentSize.height + 130);
    //重新监听
    [self addObserverForWebViewContentSize];
}
//-(void)zanAction{
//    
//
//}

-(void)weiAction{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"wx4e800b840d82e365" shareText:[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html",self.myId] shareImage:[UIImage imageNamed:@"icon-76.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite, nil] delegate:self];
}


-(UIButton *)cerateBtn{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor clearColor];
    [self.footView addSubview:btn];
    btn.contentMode = UIViewContentModeScaleAspectFit;//按比例充满
    return btn;
}



//移除观察者
- (void)removeObserverForWebViewContentSize {
    [self.footView removeFromSuperview];
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
    
}
//添加监听
- (void)addObserverForWebViewContentSize {
    [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:nil];
}

//正在加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
        //创建UIActivityIndicatorView背底半透明View
        self.myView = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.myView setBackgroundColor:[UIColor blackColor]];
        [self.myView setAlpha:0.5];
        [self.view addSubview:self.myView];
        [_myView release];
    
        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:self.view.bounds];
        [self.activityIndicator setCenter:self.myView.center];
        [self.activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
        [self.view addSubview:self.activityIndicator];
        [self.activityIndicator startAnimating];
        [_activityIndicator release];
    
}


//html加载完后触发
-(void)webViewDidFinishLoad:(UIWebView*)webView{
    [self.activityIndicator stopAnimating];
    [self.myView removeFromSuperview];
    //改变大小
    NSString *js= @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth;"
    "var maxwidth = %f;"
    "for(i=0;i <document.images.length;i++){"
    "myimg = document.images[i];"
    "if(myimg.width > maxwidth){"
    "oldwidth = myimg.width;"
    "myimg.width = %f;"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    //修改字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '110%'"];
    //背景颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
    //字体颜色
    // [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
  
}
//当请求页面出现错误的时候，我们给予提示
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertController *alart = [UIAlertController alertControllerWithTitle:@"" message:@"网络不给力😂" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alart animated:YES completion:nil];
    //自动消失的思路就是添加一个计时器,设置计时器的时间,让UIAlertController 通过dismiss消失
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(creatAlert:) userInfo:alart repeats:NO];
}
//定时器方法
- (void)creatAlert:(NSTimer *)timer{
    UIAlertController *alert = [timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
}


-(void)itemOne{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"wx4e800b840d82e365" shareText:[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html",self.myId] shareImage:[UIImage imageNamed:@"icon-76.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite, nil] delegate:self];

}
-(void)itemTwo{
    
}



-(void)leftAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
